﻿#region Copyright(c) 2006 ZO, All right reserved.
// -----------------------------------------------------------------------------
// Copyright(c) 2006 ZO, All right reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//   1.  Redistribution of source code must retain the above copyright notice,
//       this list of conditions and the following disclaimer.
//   2.  Redistribution in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//   3.  The name of the author may not be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// -----------------------------------------------------------------------------
#endregion


#region Using directives

using ZO.SmartCore.Helpers;
using ZO.SmartCore.Xml;

#endregion

namespace ZO.SmartCore.Protocols.XMPP.Protocol.Common
{
  /// <summary>
    /// Represents an error within a Stanza .
    /// </summary>
    public class StanzaError : Element
    {   

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="StanzaError"/> class.
        /// </summary>
        public StanzaError()
            : base("error", "", UriList.XmppStream, "stream", UriList.JabberClient)
        {
        } // Error

        /// <summary>
        /// Initializes a new instance of the <see cref="StanzaError"/> class.
        /// </summary>
        /// <param name="code">The code.</param>
        public StanzaError(StanzaErrorCode code)
            : this()
        {
            this.Code = code;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="StanzaError"/> class.
        /// </summary>
        /// <param name="condition">The condition.</param>
        public StanzaError(StanzaErrorCondition condition)
            : this()
        {
            this.Condition = condition;
        }
        #endregion

        #region Destructor

        #endregion

        #region Fields

        #endregion

        #region Events

        #endregion

        #region Operators

        #endregion

        #region Properties
        /// <summary>
        /// Get The Xmpp Error Type
        /// </summary>
        public StanzaErrorType Type
        {
            get { return (StanzaErrorType)this.GetAttributeAsEnum("type", typeof(StanzaErrorType)); }
            private set { this.SetAttribute("type", ReflectionHelper.GetDescription(value)); }
        }

        /// <summary>
        /// Gets the Xmpp Error code.
        /// </summary>
        /// <value>The code.</value>
        public StanzaErrorCode Code
        {
            get
            {
                return (StanzaErrorCode)this.GetAttributeAsInteger("code");
            }
            set
            {
                string stringValue = ReflectionHelper.GetDescription(value);
                this[stringValue] = new Element(stringValue, "", UriList.StanzaErrors);

                switch (value)
                {
                    case StanzaErrorCode.BadRequest:
                        this.Type = StanzaErrorType.Modify;
                        break;

                    case StanzaErrorCode.Conflict:
                        this.Type = StanzaErrorType.Cancel;
                        break;


                    case StanzaErrorCode.NotImplemented:
                        this.Type = StanzaErrorType.Cancel;
                        break;


                    case StanzaErrorCode.Forbidden:
                        this.Type = StanzaErrorType.Auth;
                        break;

                    case StanzaErrorCode.InternalServerError:
                        this.Type = StanzaErrorType.Wait;
                        break;

                    case StanzaErrorCode.NotFound:
                        this.Type = StanzaErrorType.Cancel;
                        break;

                    case StanzaErrorCode.NotAcceptable:
                        this.Type = StanzaErrorType.Modify;
                        break;

                    case StanzaErrorCode.NotAllowed:
                        this.Type = StanzaErrorType.Cancel;
                        break;

                    case StanzaErrorCode.NotAuthorized:
                        this.Type = StanzaErrorType.Auth;
                        break;

                    case StanzaErrorCode.PaymentRequired:
                        this.Type = StanzaErrorType.Auth;
                        break;

                    case StanzaErrorCode.Redirect:
                        this.Type = StanzaErrorType.Modify;
                        break;

                    case StanzaErrorCode.RegistrationRequired:
                        this.Type = StanzaErrorType.Auth;
                        break;

                    case StanzaErrorCode.RemoteServerTimeout:
                        this.Type = StanzaErrorType.Wait;
                        break;

                    case StanzaErrorCode.ServiceUnavailable:
                        this.Type = StanzaErrorType.Cancel;
                        break;

                    case StanzaErrorCode.Disconnected:
                        this.Type = StanzaErrorType.Cancel;
                        break;

                    case StanzaErrorCode.RemoteServerError:
                        this.Type = StanzaErrorType.Wait;
                        break;

                    case StanzaErrorCode.RequestTimeout:
                        this.Type = StanzaErrorType.Wait;
                        break;

                }
                if (value == StanzaErrorCode.None)
                {
                    this.RemoveAttribute("code");
                }
                else
                {
                    this.SetAttributeAsInteger("code", (int)value);
                }
            }
        }

        /// <summary>
        /// Gets text description of the error.
        /// </summary>
        /// <value>The message.</value>
        public string Message
        {
            get
            {
                return this.Value;
            }
            set
            {
                this.Value = value;
            }
        }

        /// <summary>
        /// Gets or sets the Error condition.
        /// </summary>
        /// <value>The condition.</value>
        public StanzaErrorCondition Condition
        {
            get
            {
                if (this.HasChildElement("bad-request"))
                {
                    return StanzaErrorCondition.BadRequest;
                } // if
                if (this.HasChildElement("conflict"))
                {
                    return StanzaErrorCondition.Conflict;
                } // if
                if (this.HasChildElement("feature-not-implemented"))
                {
                    return StanzaErrorCondition.FeatureNotImplemented;
                } // if
                if (this.HasChildElement("forbidden"))
                {
                    return StanzaErrorCondition.Forbidden;
                } // if
                if (this.HasChildElement("gone"))
                {
                    return StanzaErrorCondition.Gone;
                } // if
                if (this.HasChildElement("internal-server-error"))
                {
                    return StanzaErrorCondition.InternalServerError;
                } // if
                if (this.HasChildElement("item-not-found"))
                {
                    return StanzaErrorCondition.ItemNotFound;
                } // if
                if (this.HasChildElement("jid-malformed"))
                {
                    return StanzaErrorCondition.JIDMalformed;
                } // if
                if (this.HasChildElement("not-acceptable"))
                {
                    return StanzaErrorCondition.NotAcceptable;
                } // if
                if (this.HasChildElement("not-authorized"))
                {
                    return StanzaErrorCondition.NotAuthorized;
                } // if
                if (this.HasChildElement("payment-required"))
                {
                    return StanzaErrorCondition.PaymentRequired;
                } // if
                if (this.HasChildElement("recipient-unavailable"))
                {
                    return StanzaErrorCondition.RecipientUnavailable;
                } // if
                if (this.HasChildElement("redirect"))
                {
                    return StanzaErrorCondition.Redirect;
                } // if
                if (this.HasChildElement("registration-required"))
                {
                    return StanzaErrorCondition.RegistrationRequired;
                } // if
                if (this.HasChildElement("remote-server-not-found"))
                {
                    return StanzaErrorCondition.RemoteServerNotFound;
                } // if
                if (this.HasChildElement("remote-server-timeout"))
                {
                    return StanzaErrorCondition.RemoteServerTimeout;
                } // if
                if (this.HasChildElement("resource-constraint"))
                {
                    return StanzaErrorCondition.ResourceConstraint;
                } // if
                if (this.HasChildElement("service-unavailable"))
                {
                    return StanzaErrorCondition.ServiceUnavailable;
                } // if
                if (this.HasChildElement("subscription-required"))
                {
                    return StanzaErrorCondition.SubscriptionRequired;
                } // if
                if (!this.HasChildElement("undefined-condition") && this.HasChildElement("unexpected-request"))
                {
                    return StanzaErrorCondition.UnexpectedRequest;
                } // if
                return StanzaErrorCondition.UndefinedCondition;

            } // get
            set
            {
                string stringValue = ReflectionHelper.GetDescription(value);
                this[stringValue] = new Element(stringValue, "", UriList.StanzaErrors);

                switch (value)
                {
                    case StanzaErrorCondition.BadRequest:
                        this.SetAttributeAsInteger("code", (int)StanzaErrorCode.BadRequest);
                        this.Type = StanzaErrorType.Modify;
                        break;

                    case StanzaErrorCondition.Conflict:
                        this.SetAttributeAsInteger("code", (int)StanzaErrorCode.Conflict);
                        this.Type = StanzaErrorType.Cancel;
                        break;

                    case StanzaErrorCondition.FeatureNotImplemented:
                        this.SetAttributeAsInteger("code", (int)StanzaErrorCode.NotImplemented);
                        this.Type = StanzaErrorType.Cancel;
                        break;

                    case StanzaErrorCondition.Forbidden:
                        this.SetAttributeAsInteger("code", (int)StanzaErrorCode.Forbidden);
                        this.Type = StanzaErrorType.Auth;
                        break;

                    case StanzaErrorCondition.Gone:
                        this.SetAttributeAsInteger("code", (int)StanzaErrorCode.Redirect);
                        this.Type = StanzaErrorType.Modify;
                        break;

                    case StanzaErrorCondition.InternalServerError:
                        this.SetAttributeAsInteger("code", (int)StanzaErrorCode.InternalServerError);
                        this.Type = StanzaErrorType.Wait;
                        break;

                    case StanzaErrorCondition.ItemNotFound:
                        this.SetAttributeAsInteger("code", (int)StanzaErrorCode.NotFound);
                        this.Type = StanzaErrorType.Cancel;
                        break;

                    case StanzaErrorCondition.JIDMalformed:
                        this.SetAttributeAsInteger("code", (int)StanzaErrorCode.BadRequest);
                        this.Type = StanzaErrorType.Modify;
                        break;

                    case StanzaErrorCondition.NotAcceptable:
                        this.SetAttributeAsInteger("code", (int)StanzaErrorCode.NotAcceptable);
                        this.Type = StanzaErrorType.Modify;
                        break;

                    case StanzaErrorCondition.NotAllowed:
                        this.SetAttributeAsInteger("code", (int)StanzaErrorCode.NotAllowed);
                        this.Type = StanzaErrorType.Cancel;
                        break;

                    case StanzaErrorCondition.NotAuthorized:
                        this.SetAttributeAsInteger("code", (int)StanzaErrorCode.NotAuthorized);
                        this.Type = StanzaErrorType.Auth;
                        break;

                    case StanzaErrorCondition.PaymentRequired:
                        this.SetAttributeAsInteger("code", (int)StanzaErrorCode.PaymentRequired);
                        this.Type = StanzaErrorType.Auth;
                        break;

                    case StanzaErrorCondition.RecipientUnavailable:
                        this.SetAttributeAsInteger("code", (int)StanzaErrorCode.NotFound);
                        this.Type = StanzaErrorType.Wait;
                        break;

                    case StanzaErrorCondition.Redirect:
                        this.SetAttributeAsInteger("code", (int)StanzaErrorCode.Redirect);
                        this.Type = StanzaErrorType.Modify;
                        break;

                    case StanzaErrorCondition.RegistrationRequired:
                        this.SetAttributeAsInteger("code", (int)StanzaErrorCode.RegistrationRequired);
                        this.Type = StanzaErrorType.Auth;
                        break;

                    case StanzaErrorCondition.RemoteServerNotFound:
                        this.SetAttributeAsInteger("code", (int)StanzaErrorCode.NotFound);
                        this.Type = StanzaErrorType.Cancel;
                        break;

                    case StanzaErrorCondition.RemoteServerTimeout:
                        this.SetAttributeAsInteger("code", (int)StanzaErrorCode.RemoteServerTimeout);
                        this.Type = StanzaErrorType.Wait;
                        break;

                    case StanzaErrorCondition.ResourceConstraint:
                        this.SetAttributeAsInteger("code", (int)StanzaErrorCode.InternalServerError);
                        this.Type = StanzaErrorType.Wait;
                        break;

                    case StanzaErrorCondition.ServiceUnavailable:
                        this.SetAttributeAsInteger("code", (int)StanzaErrorCode.ServiceUnavailable);
                        this.Type = StanzaErrorType.Cancel;
                        break;

                    case StanzaErrorCondition.SubscriptionRequired:
                        this.SetAttributeAsInteger("code", (int)StanzaErrorCode.RegistrationRequired);
                        this.Type = StanzaErrorType.Auth;
                        break;

                    case StanzaErrorCondition.UndefinedCondition:
                        //this.SetAttributeAsInteger("code", (int)ErrorCode.Conflict);
                        break;

                    case StanzaErrorCondition.UnexpectedRequest:
                        this.SetAttributeAsInteger("code", (int)StanzaErrorCode.BadRequest);
                        this.Type = StanzaErrorType.Wait;
                        break;

                }
            } // set
        } // Condition
        #endregion

        #region Methods

        #endregion
    }
}
